#include<stdio.h>
#include<stdlib.h>

int n;
struct node
{
	int value;
	struct node* next;
};

struct node *first=NULL;

struct node* insert_node(struct node *s,int val)
{
	s=malloc(sizeof(struct node));
	s->value=val;
	s->next=first;
	first=s;
	return first;
}

void display_list()
{
	struct node *p;
	for(p=first;p!=NULL;p=p->next)
	{
		printf(" %d ",p->value);
	}
}

void count()       //counts the number of occurences of the given element
{
	int k,j,count;
	struct node *p;
	count=0;
	printf("Enter the element whose count is to be determined:\n");
	scanf("%d",&k);
	for(p=first;p!=NULL;p=p->next)
	{
		if (p->value==k)
		{
			count++;
		}	
	}
	printf("The count of the element %d is %d\n",k,count);	
}

void indexing()     //Prints the index of an element
{
	int num,i,count;
	count=0;
	struct node *p;
	printf("Enter the element whose index is to be printed:");
	scanf("%d",&num);
	for (p=first;p!=NULL;p=p->next)
	{	
		count++;
		if (p->value==num)
		{
			printf("The index of %d is %d\n",num,count-1);	
			break;
		}
		
	}
	
}

void append()          //appends an element at the end of the list
{
	struct node *a,*p;
	a=malloc(sizeof(struct node));
	p=malloc(sizeof(struct node));
	int num,i;
	printf("Enter the number to be appended to the end of the list:");
	scanf("%d",&num);
	a->next=NULL;
	a->value=num;
	p=first;
	for (i=0;i<n;i++)
	{
		p=p->next;
	}
	p->next=a;
	printf("%d",first->value);
	display_list();
}

void main()
{
	int n,x,i,function_name,ans;
	struct node *new_node,*p;	
	printf("Enter the number of elements in the list:");
	scanf("%d",&n);
	printf("Enter the elements of the list in succession:\n");
	for(i=1;i<=n;i++)
	{
		
		scanf("%d",&x);
		first=insert_node(first,x);
	}
	do
	{
	printf("ENTER THE FUNCTION TO BE IMPLEMENTED:\n1=Display list\n2=count\n3=indexing\n4=append\n");
	scanf("%d",&function_name);
	switch (function_name)
	{	
		case 1:display_list();
			break;
		case 2:count();
			break;
		case 3:indexing();
			break;
		case 4:append();	
			break;
		
	}
	printf("Do you wish to continue?\nType 1 if Yes and 2 if no.\n");
	scanf("%d",&ans);
	}while (ans==1);
		
}


